package com.ssy.lingxi.contract.serviceimpl.mobile;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.contract.entity.SignatureAuth;
import com.ssy.lingxi.contract.entity.SignatureAuthLog;
import com.ssy.lingxi.contract.model.constant.CommonBooleanEnum;
import com.ssy.lingxi.contract.model.constant.sign.SignatureAuthStateEnum;
import com.ssy.lingxi.contract.model.vo.sign.response.MobileSignatureAuthStateResponse;
import com.ssy.lingxi.contract.repository.SignatureAuthLogRepository;
import com.ssy.lingxi.contract.repository.SignatureAuthRepository;
import com.ssy.lingxi.contract.service.mobile.IMobileSignatureAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 电子签章服认证务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2020/9/1
 */
@Slf4j
@Service
public class MobileSignatureAuthServiceImpl implements IMobileSignatureAuthService {

    @Resource
    private SignatureAuthRepository signatureAuthRepository;
    @Resource
    private SignatureAuthLogRepository signatureAuthLogRepository;

    @Override
    public Wrapper<MobileSignatureAuthStateResponse> getState(UserLoginCacheDTO sysUser) {
        MobileSignatureAuthStateResponse response = new MobileSignatureAuthStateResponse();

        SignatureAuth signatureAuth = signatureAuthRepository.findByMemberId(sysUser.getMemberId());
        if (Objects.isNull(signatureAuth) || CommonBooleanEnum.NO.getCode().equals(signatureAuth.getState())) {
            response.setState(SignatureAuthStateEnum.NOT_APPLYING.getCode());
            return Wrapper.success(response);
        }

        SignatureAuthLog signatureAuthLog = signatureAuthLogRepository.findById(signatureAuth.getSignatureAuthLogId()).orElse(null);
        if (Objects.isNull(signatureAuthLog)) {
            response.setState(SignatureAuthStateEnum.NOT_APPLYING.getCode());
            return Wrapper.success(response);
        }

        response.setMemberId(sysUser.getMemberId());
        response.setState(signatureAuthLog.getState());

        return Wrapper.success(response);
    }
}